<template>
  <div class="app-container">
    <el-row style="margin-bottom: 0">
      <!-- 查询表单 -->
      <el-col :span="12">
        <el-form :inline="true" size="medium">
          <el-form-item label="图书">
            <el-input
              clearable
              v-model="searchObj.isbn"
              class="inline-input"
              placeholder="ISBN书号"
              value-key="no"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              clearable
              v-model="searchObj.name"
              class="inline-input"
              placeholder="图书名"
              value-key="name"
              style="width:150px"
            />
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh-right" @click="resetData()">重置</el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </el-col>
      <!-- 工具栏 -->
      <el-col :span="12" style="text-align: right">
        <div style="margin-top: 4px">
          <el-form :inline="true"
                   :rules="rules"
                   :model="employee"
                   size="medium"
                   ref="employeeForm"
          >
            <el-form-item label="员工" prop="no">
              <el-input
                clearable
                v-model="employee.no"
                class="inline-input"
                placeholder="工号"
                style="width:150px"
              />
            </el-form-item>
            <el-form-item prop="name">
              <el-input
                clearable
                v-model="employee.name"
                class="inline-input"
                placeholder="姓名"
                style="width:150px"
              />
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-col :span="24">
        <el-table
          :data="list"
          :header-cell-style="{'text-align':'center'}"
          fit
          stripe
          tooltip-effect="light"
          v-loading="loading"
          highlight-current-row
          element-lodaing-text="数据加载中"
        >
          <el-table-column type="index" label="#" align="center">
            <template slot-scope="scope">
              {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="isbn" label="ISBN书号" sortable align="center"/>
          <el-table-column prop="name" label="图书名" sortable align="center"/>
          <el-table-column prop="author" label="作者" sortable align="center"/>
          <el-table-column prop="press" label="出版社" sortable align="center"/>
          <el-table-column prop="leftNum" label="可借数量" align="center"/>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="success" size="small" plain
                         @click="borrowBook(scope.row)"
              >借阅
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页组件 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { bookApi, borrowApi, employeeApi } from '@/api'

export default {
  // 定义数据模型
  data() {
    return {
      loading: true, // 加载遮罩
      list: [], // 图书列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件
      employee: {}, // 员工对象
      borrow: {}, // 借阅对象
      rules: {
        no: [{ required: true, message: '请输入员工号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  // 页面渲染后获取数据
  created() {
    this.fetchData()
  },
  // 定义方法
  methods: {
    fetchData() {
      // 开始加载
      this.loading = true
      // 调用api
      bookApi.queryBorrowByPage(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // 结束加载
        this.loading = false
      })
    },
    // 更改每页记录数
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 更改页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 借阅图书
    borrowBook(book) {
      this.$refs.employeeForm.validate(valid => {
        if (valid) {
          employeeApi.queryByNo(this.employee.no)
            .then(response => {
              if (response.data) {
                console.log("有该员工")
                let employee = response.data.item
                this.borrow.bookId = book.id
                this.borrow.isbn = book.isbn
                this.borrow.bookName = book.name
                this.borrow.employeeId = employee.id
                this.borrow.no = employee.no
                this.borrow.employeeName = employee.name
                borrowApi.insert(this.borrow).then(response => {
                  this.fetchData()
                  this.$message.success(response.message)
                })
              } else {
                this.$message.warning('没有该员工')
              }
            })
        } else {
          this.$message.warning('请完成必填项')
        }
      })

    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/table_head_align.scss";
</style>
